# -*- coding: utf-8 -*-

# Define here the models for your scraped items
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/items.html

import scrapy
import re

from scrapy.loader.processors import MapCompose, TakeFirst, Join


def item_views(value=""):
    return value.replace(" ", "")


def item_cate_count(value=""):
    return re.findall(r'.*?(\d+).*', value)


def item_hoting_rate(value=""):
    return re.findall(r'.*?(\d+).*', value)


class ScrapyNddItem(scrapy.Item):
    """
    所有提取到的值都存储在列表里，待处理
    input_processor用来处理获取的内容，不指定则为默认值
    output_processor用来最后输出，不指定则为默认值
    MapCompose函数里可添加任意多函数，每个函数都会作用于列表里的每个元素，类似map函数
    """
    # 配置默认的output_processor,取出list中的第一个元素
    default_output_processor = TakeFirst()

    video_id = scrapy.Field(
        output_processor=default_output_processor
    )

    category = scrapy.Field(
        output_processor=Join(",")
    )

    type = scrapy.Field(
        output_processor=Join(",")
    )

    title = scrapy.Field(
        output_processor=default_output_processor
    )

    screenshot_url_1 = scrapy.Field(
        output_processor=default_output_processor
    )

    screenshot_url_2 = scrapy.Field(
        output_processor=default_output_processor
    )

    screenshot_url_3 = scrapy.Field(
        output_processor=default_output_processor
    )

    screenshot_url_4 = scrapy.Field(
        output_processor=default_output_processor
    )

    screenshot_url_5 = scrapy.Field(
        output_processor=default_output_processor
    )

    videos_screenshots = scrapy.Field(
        output_processor=default_output_processor
    )

    video_alt_url = scrapy.Field(
        output_processor=default_output_processor
    )

    video_url = scrapy.Field(
        output_processor=default_output_processor
    )

    download_url = scrapy.Field(
        output_processor=default_output_processor
    )

    views = scrapy.Field(
        input_processor=MapCompose(item_views),
        output_processor=default_output_processor
    )

    md5 = scrapy.Field(
        output_processor=default_output_processor
    )

    size = scrapy.Field(
        output_processor=default_output_processor
    )

    duration = scrapy.Field(
        output_processor=default_output_processor
    )

    status = scrapy.Field(
        output_processor=default_output_processor
    )

    tag = scrapy.Field(
        output_processor=Join(",")
    )

    vip = scrapy.Field(
        output_processor=default_output_processor
    )

    create_time = scrapy.Field(
        output_processor=default_output_processor
    )


class ScrapyNddCategoryItem(scrapy.Item):

    default_output_processor = TakeFirst()

    cate_url = scrapy.Field(
        output_processor=default_output_processor
    )

    cate_name = scrapy.Field(
        output_processor=default_output_processor
    )

    cate_count = scrapy.Field(
        input_processor=MapCompose(item_cate_count),
        output_processor=default_output_processor
    )

    hoting_rate = scrapy.Field(
        input_processor=MapCompose(item_hoting_rate),
        output_processor=default_output_processor
    )

    item_href = scrapy.Field(
        output_processor=default_output_processor
    )

    create_time = scrapy.Field(
        output_processor=default_output_processor
    )

    curr_page = scrapy.Field(
        output_processor=default_output_processor
    )

    curr_page_item = scrapy.Field(
        output_processor=default_output_processor
    )

    complete = scrapy.Field(
        output_processor=default_output_processor
    )


class ScrapyNddNewUrlItem(scrapy.Item):
    default_output_processor = TakeFirst()

    new_url = scrapy.Field(
        output_processor=default_output_processor
    )


class ScrapyCoinItem(scrapy.Item):
    default_output_processor = TakeFirst()

    coin_price = scrapy.Field(
        output_processor=default_output_processor
    )
